package cn.liuyi.weiyi;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.tools.CustomerHttpClient;
import cn.edu.pku.tools.MD5;
import cn.edu.pku.tools.SharedPreferencesHelper;
import cn.edu.pku.tools.StreamTool;
import cn.edu.pku.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

/**
 * @author vector
 *
 */
public class RgCompleteActivity extends SherlockActivity implements OnClickListener{
	
	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	Bitmap captcha_pic;
	String response, captcha;
	Context context;
	EditText register_pwd_et, register_pwd_again_et;
	Button register_end_btn;
	static final int POST_PWD = 0; 
	JSONObject jsonInfo;
	String pwd, pwd2;
	String userType = "P",phoneNumber;
	SharedPreferencesHelper spHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_rg_complete);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		Intent intent = this.getIntent();
		phoneNumber = intent.getStringExtra("phoneNumber");
		this.spHelper = new SharedPreferencesHelper(this.context);
		initTitleBar();
		initView();
		initEventListener();
	
	}
	
	private void initTitleBar(){
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		actionBar.setDisplayUseLogoEnabled(false);
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.register_action_bar3, null);
		actionBar.setCustomView(title_bar,lp);
	}
	private void initView(){
		register_pwd_et = (EditText) findViewById(R.id.register_pwd);
		register_pwd_again_et = (EditText) findViewById(R.id.register_pwd_again);
		register_end_btn = (Button) findViewById(R.id.rg_end_btn);
		
		
	}

	private void initEventListener(){
		register_end_btn.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch( v.getId()){
		case R.id.rg_end_btn:
			pwd = register_pwd_et.getText().toString();
			pwd2 = register_pwd_again_et.getText().toString();
			if(pwd.isEmpty()||pwd2.isEmpty()){
				Toast.makeText(context, "请输入密码", Toast.LENGTH_LONG).show();
			}else if(!pwd.equals(pwd2)){
				Toast.makeText(context, "输入两次密码不一致，请重试", Toast.LENGTH_LONG).show();
			}else{
				register(MD5.getMD5(pwd));
			}
			break;
		default:
			break;
		}
		
	}
	
	
	private void register(String pwd){
		Toast.makeText(context, "check begin", Toast.LENGTH_SHORT).show();
		final String pwd_ = pwd;
		RequestParams params = new RequestParams(); 
		params.add("password", pwd_);
		params.add("userType", userType);
		YJRHttpClient.post(context,"/user/doRegister", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();
							spHelper.putString("phoneNumber", phoneNumber);
							spHelper.putString("password", pwd_);
							Intent intent = new Intent(RgCompleteActivity.this,LoginActivity.class);
							startActivity(intent);
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
	
	
}